<template>
	<el-container>
		<el-aside width="240px">
			<el-container>
				<el-main class="nopadding">
					<blCategoryTree model="ShortTemplate" title="短链分类" ref="group" @node-click="groupClick" />
				</el-main>
			</el-container>
		</el-aside>
		<el-container>
			<el-header>
				<div class="left-panel">
					<el-button type="primary" icon="el-icon-plus" @click="add">创建短链模板</el-button>
					<div style="width: 15px;">&nbsp;</div>
					<el-date-picker v-model="search.dateRange" type="daterange" align="right" unlink-panels
						range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" value-format="YYYY-MM-DD">
					</el-date-picker>
					<div class="right-panel-search" style="padding-left: 15px;">
						<el-input v-model="search.keyword" placeholder="模板名称/模板内容" clearable></el-input>
						<el-button type="primary" icon="el-icon-search" @click="upsearch">查询</el-button>
					</div>
				</div>
				<div class="right-panel">
					<div class="right-panel-search">

					</div>
				</div>
			</el-header>
			<div v-if="true">
				<el-card shadow="never">
					<sc-select-filter :data="selectFilter" :selected-values="selectedValues" :label-width="80"
						@on-change="stateChange"></sc-select-filter>
				</el-card>
			</div>
			<el-main class="nopadding">
				<scTable :params="{selectFilter:selectedValues}" ref="table" :apiObj="apiObj.list"
					@selection-change="selectionChange" stripe remoteSort remoteFilter>
					<el-table-column label="名称" prop="short_name" width="250">
						<template #default="{row}">
							<div @click="table_edit(row)">
								{{row.short_name}}
							</div>
						</template>
					</el-table-column>
					<el-table-column label="短链内容" prop="short_content" min-width="250" />
					<el-table-column label="模板CODE" align="center" prop="short_template_code" width="150" />
					<el-table-column label="短链平台" align="center" prop="short_platform" width="90" />
					<el-table-column label="分类" align="center" prop="category_id" width="150">
						<template #default="{row}">
							{{(row.category && row.category.cat_name) || '-'}}
						</template>
					</el-table-column>

					<el-table-column label="状态" prop="short_status" width="120">
						<template #default="scope">
							<el-icon v-if="['审核通过'].includes(scope.row.short_status)"
								style="color: #67C23A;"><el-icon-circle-check-filled /></el-icon>
							<el-icon v-else style="color: #F46061;"><el-icon-circle-close-filled /></el-icon>
							{{scope.row.short_status}}
						</template>
					</el-table-column>

					<el-table-column label="创建时间" prop="created_at" width="150" />
					<el-table-column label="操作" fixed="right" align="right" width="90">
						<template #default="{row}">
							<el-button-group>
								<el-button @click="table_edit(row)" link type="primary" size="small">编辑</el-button>
							</el-button-group>
						</template>
					</el-table-column>
				</scTable>
			</el-main>
		</el-container>
	</el-container>

	<save-dialog v-if="dialog.save" ref="saveDialog" @success="handleSuccess" @closed="dialog.save=false"></save-dialog>
</template>

<script>
	import saveDialog from './save'

	import scSelectFilter from '@/components/scSelectFilter'

	export default {
		name: 'sms.template',
		components: {
			saveDialog,
			scSelectFilter
		},
		data() {
			return {
				selectFilter: [{
					title: "状态",
					key: "short_status",
					options: [{
							label: "全部",
							value: null
						},
						{
							label: "审核通过",
							value: "审核通过",
						},
						{
							label: "审核驳回",
							value: "审核驳回",
						},
						{
							label: "审核中",
							value: "审核中",
						},
						{
							label: "待审核",
							value: "待审核",
						}
					]
				}],
				selectedValues: {

				},
				dialog: {
					save: false
				},
				apiObj: this.$API.system.shortTemplate,
				selection: [],
				search: {
					keyword: null,
					dateRange: null
				}
			}
		},
		methods: {
			stateChange(e) {
				var params = {
					selectFilter: e
				}
				this.$refs.table.upData(params)
			},
			//添加
			add() {
				this.dialog.save = true
				this.$nextTick(() => {
					const checkedNode = this.$refs.group.getCurrentNode();
					this.$refs.saveDialog.open().setData({
						category_id: checkedNode.id || null
					});
				})
			},
			//编辑
			table_edit(row) {
				this.dialog.save = true
				this.$nextTick(() => {
					this.$refs.saveDialog.open('edit').setData(row)
				})
			},
			//查看
			table_show(row) {
				this.dialog.save = true
				this.$nextTick(() => {
					this.$refs.saveDialog.open('show').setData(row)
				})
			},
			//删除
			async table_del(row, index) {
				var reqData = {
					id: row.id
				}
				var res = await this.apiObj.remove.post(reqData);
				if (res.code == 0) {
					this.$refs.table.tableData.splice(index, 1);
					this.$message.success(res.msg)
				} else {
					this.$alert(res.message, "提示", {
						type: 'error'
					})
				}
			},
			//批量删除
			async batch_del() {
				this.$confirm(`确定删除选中的 ${this.selection.length} 项吗？`, '提示', {
					type: 'warning'
				}).then(() => {
					const loading = this.$loading();
					this.selection.forEach(item => {
						this.$refs.table.tableData.forEach((itemI, indexI) => {
							if (item.id === itemI.id) {
								this.$refs.table.tableData.splice(indexI, 1)
							}
						})
					})
					loading.close();
					this.$message.success("操作成功")
				}).catch(() => {

				})
			},
			//表格选择后回调事件
			selectionChange(selection) {
				this.selection = selection;
			},
			//树点击事件
			groupClick(data) {
				var params = {
					category_id: data.id
				}
				this.$refs.table.upData(params)
			},
			//搜索
			upsearch() {
				this.$refs.table.upData(this.search)
			},
			//本地更新数据
			handleSuccess(data, mode) {
				if (mode == 'add') {
					this.$refs.table.refresh()
				} else if (mode == 'edit') {
					this.$refs.table.refresh()
				}
			}
		}
	}
</script>

<style>
</style>
